﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using GuestList.Models;
using System.Collections.ObjectModel;
using GuestList.Commands;
using System.Windows.Controls;

namespace GuestList.ViewModels
{
    public class InvitationVM : WindowVM
    {
        private Invitation _model;
        private static readonly ObservableCollection<InvitationType> _invitationTypes = new ObservableCollection<InvitationType>
        {
            InvitationType.Ceremony,
            InvitationType.ReceptionOnly
        };
        private Person _selectedOtherGuest = null;
        
        public ObservableCollection<InvitationType> InvitationTypes
        {
            get { return _invitationTypes; }
        }

        public Invitation Model
        {
            get { return _model; }
            set
            {
                _model = value;
                Notify("Model");
            }
        }

        public Person SelectedOtherGuest
        {
            get { return _selectedOtherGuest; }
            set
            {
                _selectedOtherGuest = value;
                Notify("SelectedOtherGuest");
            }
        }

        public InvitationVM(Window window) : base(window)
        {

        }

        public string FirstName
        {
            get { return _model.PrimaryGuest.FirstName; }
            set
            {
                _model.PrimaryGuest.FirstName = value;
                Notify("FirstName");
            }
        }

        public string LastName
        {
            get { return _model.PrimaryGuest.LastName; }
            set
            {
                _model.PrimaryGuest.LastName = value;
                Notify("LastName");
            }
        }

        public string Name
        {
            get { return string.Format("{0} {1}", _model.PrimaryGuest.FirstName, _model.PrimaryGuest.LastName); }
        }

        public string Address1
        {
            get { return _model.Address1; }
            set
            {
                _model.Address1 = value;
                Notify("Address1");
            }
        }

        public InvitationVM(Invitation model) : base(null)
        {
            _model = model;
        }

        protected override Window GetNewWindowInstance()
        {
            return new InvitationWindow();
        }

        public RelayCmd PrintSelectedInvitationCmd { get { return new RelayCmd(PrintSelectedInvitation, () => true); } }

        private void PrintSelectedInvitation()
        {
            new PrintPreviewVM(new PrintPreviewWindow()) { Model = Model }.Show();
        }
        
        public RelayCmd OpenInvitationCmd
        {
            get
            {
                return new RelayCmd<Invitation>
                (
                    x => Show(),
                    () => true
                );
            }
        }

        public RelayCmd AddOtherGuestCmd
        {
            get
            {
                return new RelayCmd
                (
                    () => { Model.OtherGuests.Add(new Person()); },
                    () => true
                );
            }
        }

        public RelayCmd RemoveOtherGuestCmd
        {
            get
            {
                return new RelayCmd
                (
                    () => Model.OtherGuests.Remove(SelectedOtherGuest),
                    () => SelectedOtherGuest != null
                );
            }
        }
    }
}
